package com.dragonwu.handler;

import com.alibaba.fastjson.JSON;
import com.dragonwu.model.R;
import com.dragonwu.utils.WebUtil;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * author: Dragon Wu
 * date: 2022-07-13 13:32
 */
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        R<String> result = R.restResult(null, HttpStatus.FORBIDDEN.value(), "禁止访问");
        String json = JSON.toJSONString(result);
        //处理异常，返回结果
        WebUtil.renderString(httpServletResponse, json);
    }
}
